Page({
  data: {
    userInfo: {
      avatar: '/images/avatar.png',
      name: '爱学习的西西',
      phone: '138****8888',
      email: 'study@example.com',
      grade: '六年级',
      school: '北京市第一小学'
    },
    settingsItems: [
      {
        id: 'personal',
        title: '个人资料',
        icon: '/images/profile.svg',
        showArrow: true,
        type: 'navigate'
      },
      {
        id: 'background',
        title: '背景设置',
        icon: '/images/settings.svg',
        value: '浅色模式',
        showArrow: true,
        type: 'select'
      },
      {
        id: 'font',
        title: '字号调整',
        icon: '/images/edit.svg',
        showArrow: true,
        type: 'navigate'
      },
      {
        id: 'theme',
        title: '当前模式',
        icon: '/images/settings.svg',
        value: '家长模式',
        showArrow: true,
        type: 'select'
      }
    ],
    notificationItems: [
      {
        id: 'push',
        title: '推送通知设置',
        icon: '/images/notification.svg',
        showArrow: true,
        type: 'navigate'
      },
      {
        id: 'voice',
        title: '语音开关',
        icon: '/images/settings.svg',
        value: true,
        type: 'switch'
      }
    ],
    storageItems: [
      {
        id: 'cache',
        title: '清除缓存',
        icon: '/images/clear-all.svg',
        value: '38.2MB',
        showArrow: true,
        type: 'action'
      }
    ],
    aboutItems: [
      {
        id: 'privacy',
        title: '隐私政策',
        icon: '/images/help.svg',
        showArrow: true,
        type: 'navigate'
      },
      {
        id: 'collection',
        title: '个人信息收集清单',
        icon: '/images/collection.svg',
        showArrow: true,
        type: 'navigate'
      },
      {
        id: 'about',
        title: '关于我们',
        icon: '/images/help.svg',
        showArrow: true,
        type: 'navigate'
      },
      {
        id: 'version',
        title: '检查更新',
        icon: '/images/download.svg',
        value: '●',
        showArrow: true,
        type: 'action'
      }
    ]
  },

  onLoad: function (options) {
    console.log('账号设置页面加载');
    wx.setNavigationBarTitle({
      title: '账号设置'
    });
  },

  onShow: function () {
    console.log('账号设置页面显示');
  },

  // 返回上一页
  onBack: function() {
    wx.navigateBack();
  },

  // 点击设置项
  onSettingTap: function(e) {
    const item = e.currentTarget.dataset.item;
    console.log('点击设置项:', item);

    switch(item.type) {
      case 'navigate':
        this.handleNavigate(item);
        break;
      case 'select':
        this.handleSelect(item);
        break;
      case 'switch':
        this.handleSwitch(item);
        break;
      case 'action':
        this.handleAction(item);
        break;
      default:
        console.log('未知设置项类型');
    }
  },

  // 处理导航跳转
  handleNavigate: function(item) {
    switch(item.id) {
      case 'personal':
        wx.showToast({
          title: '个人资料编辑功能开发中',
          icon: 'none'
        });
        break;
      case 'font':
        wx.showToast({
          title: '字号调整功能开发中',
          icon: 'none'
        });
        break;
      case 'push':
        wx.showToast({
          title: '推送设置功能开发中',
          icon: 'none'
        });
        break;
      case 'privacy':
        wx.showToast({
          title: '隐私政策功能开发中',
          icon: 'none'
        });
        break;
      case 'collection':
        wx.showToast({
          title: '信息收集清单功能开发中',
          icon: 'none'
        });
        break;
      case 'about':
        wx.showToast({
          title: '关于我们功能开发中',
          icon: 'none'
        });
        break;
      default:
        wx.showToast({
          title: '功能开发中',
          icon: 'none'
        });
    }
  },

  // 处理选择类设置
  handleSelect: function(item) {
    switch(item.id) {
      case 'background':
        this.showBackgroundOptions();
        break;
      case 'theme':
        this.showThemeOptions();
        break;
      default:
        wx.showToast({
          title: '功能开发中',
          icon: 'none'
        });
    }
  },

  // 处理开关类设置
  handleSwitch: function(item) {
    if (item.id === 'voice') {
      const newValue = !item.value;
      const notificationItems = this.data.notificationItems.map(notifItem => {
        if (notifItem.id === 'voice') {
          notifItem.value = newValue;
        }
        return notifItem;
      });
      
      this.setData({
        notificationItems: notificationItems
      });

      wx.showToast({
        title: newValue ? '语音已开启' : '语音已关闭',
        icon: 'success'
      });
    }
  },

  // 处理操作类设置
  handleAction: function(item) {
    switch(item.id) {
      case 'cache':
        this.clearCache();
        break;
      case 'version':
        this.checkUpdate();
        break;
      default:
        wx.showToast({
          title: '功能开发中',
          icon: 'none'
        });
    }
  },

  // 显示背景选项
  showBackgroundOptions: function() {
    wx.showActionSheet({
      itemList: ['浅色模式', '深色模式', '跟随系统'],
      success: (res) => {
        const options = ['浅色模式', '深色模式', '跟随系统'];
        const selectedOption = options[res.tapIndex];
        
        const settingsItems = this.data.settingsItems.map(item => {
          if (item.id === 'background') {
            item.value = selectedOption;
          }
          return item;
        });
        
        this.setData({
          settingsItems: settingsItems
        });

        wx.showToast({
          title: `已切换到${selectedOption}`,
          icon: 'success'
        });
      }
    });
  },

  // 显示主题选项
  showThemeOptions: function() {
    wx.showActionSheet({
      itemList: ['家长模式', '学生模式', '教师模式'],
      success: (res) => {
        const options = ['家长模式', '学生模式', '教师模式'];
        const selectedOption = options[res.tapIndex];
        
        const settingsItems = this.data.settingsItems.map(item => {
          if (item.id === 'theme') {
            item.value = selectedOption;
          }
          return item;
        });
        
        this.setData({
          settingsItems: settingsItems
        });

        wx.showToast({
          title: `已切换到${selectedOption}`,
          icon: 'success'
        });
      }
    });
  },

  // 清除缓存
  clearCache: function() {
    wx.showModal({
      title: '清除缓存',
      content: '确定要清除所有缓存数据吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '清除中...'
          });
          
          setTimeout(() => {
            wx.hideLoading();
            
            const storageItems = this.data.storageItems.map(item => {
              if (item.id === 'cache') {
                item.value = '0MB';
              }
              return item;
            });
            
            this.setData({
              storageItems: storageItems
            });

            wx.showToast({
              title: '缓存清除成功',
              icon: 'success'
            });
          }, 1500);
        }
      }
    });
  },

  // 检查更新
  checkUpdate: function() {
    wx.showLoading({
      title: '检查中...'
    });
    
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '已是最新版本',
        icon: 'success'
      });
    }, 1000);
  },

  // 退出登录
  onLogout: function() {
    wx.showModal({
      title: '退出登录',
      content: '确定要退出登录吗？',
      confirmText: '退出',
      cancelText: '取消',
      confirmColor: '#f44336',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '退出中...'
          });
          
          setTimeout(() => {
            wx.hideLoading();
            wx.reLaunch({
              url: '/pages/login/login'
            });
          }, 1000);
        }
      }
    });
  }
});